<!DOCTYPE html>
<html>

<head>
    <title>Markdown Editor</title>
    <script src="http://chst.vip:8081/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/marked/1.2.7/marked.min.js"></script>
    <link rel="stylesheet" type="text/css" href="./style.css" />
    <meta name="referrer" content="never">
</head>

<body>
    <div id="editor">
        <textarea :value="input" @input="update"></textarea>
        <div v-html="compiledMarkdown"></div>
    </div>
    <script>
        function debounce(fn, t) {
            console.log(this);
            //核心:setTimeout 如果逻辑处理在间隔时间之内,那么就要清掉上个定时器,只保留一个定时器
            let timer = null;
            return function (e) {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    //此处处理业务逻辑
                    if (fn) {
                        fn = fn.bind(this)
                        fn(e)
                    }
                }, t)
            }
        }
        new Vue({
            el: "#editor",
            data: {
                input: "# hello"
            },
            computed: {
                compiledMarkdown: function () {
                    return marked(this.input);
                }
            },
            methods: {
                update: debounce(function (e) {
                    console.log(this);
                    this.input = e.target.value;
                }, 300)
            }
        });
    </script>
</body>

</html>